import GameLogic

cont = GameLogic.getCurrentController()

#return the object for which the script has been triggered
own = cont.getOwner()

mouseOver = cont.getSensor("mouseOver")
leftClick = cont.getSensor("leftClick")

#ugly hack to solve the double triggering that occur when an object is selected.
if GameLogic.ignoreFlag:
	GameLogic.ignoreFlag = False
else:
	GameLogic.ignoreFlag = True

	#Object is already selected
	if (GameLogic.selectedObject == own):
		#We release the object on click
		if leftClick.isTriggered():
			GameLogic.selectedObject = None

			#Re-insert the object in the physics simulation
			own.restoreDynamics()
			own.setLinearVelocity([0, 0, 0])
			own.setAngularVelocity([0, 0, 0])

			print own.getName(), 'deselected'
					
	#Object is not selected
	else:
		#We set the selectObject variable to this object
		if mouseOver.isPositive() & leftClick.isTriggered():
			GameLogic.selectedObject = own

			#Remove the object from physics simulation
			own.suspendDynamics()

			print own.getName(), 'selected'		
